<?php
namespace Home\Controller;
use Think\Controller;

class AjaxDevController extends Controller{
	//Ajax入口
	function doAjax(){
		$funType = $_GET['funType'];
		$ajaxMsg = "Init massage";
		
		switch($funType){
			case "getAllDev":	$ajaxMsg = $this->getAllDev();	break;
			case "sltDev":		$keyword = $_GET['keyword'];	$ajaxMsg = $this->sltItem($keyword);	break;
		}
		$this->ajaxReturn($ajaxMsg);
		/*
		if($status == 'getDev'){
			$html = $this->devItem();
		}else if($status == 'sltItem'){
			$keyword = $_GET['keyword'];
			$html = $this->sltItem($keyword);
		}else{
			$this->error("Not found!");
		}
		$this->ajaxReturn($html);
		*/
	}
	//设备条目
	function getAllDev(){
		$dve = new \Home\Common\Device();
		$arDve = $dve->getDevice();
		
		$html = "<table cellpadding='0' cellspacing='0' id='dev-table' class='info-table'>
				<tr><th colspan='9'>公司设备条目：</th></tr>
				<tr><th width='40'>序号</th>
				<th width='50'>类型</th>
				<th width='40'>型号</th>
				<th width='60'>厂商</th>
				<th width='80'>配置</th>
				<th width='80'>Mac地址</th>
				<th width='40'>系统</th>
				<th width='60'>版本</th>
				<th width='40'>备注</th></tr>";
		foreach($arDve as $aDev){
			$aDev[4] = str_replace(array(";","；"), "<br>", $aDev[4]);
			$html .= "<tr>
			<td>$aDev[0]</td><td>$aDev[1]</td><td>$aDev[2]</td><td>$aDev[3]</td>
			<td>$aDev[4]</td><td>$aDev[5]</td><td>$aDev[6]</td><td>$aDev[7]</td>
			<td>$aDev[8]</td></tr>";
		}
		$html .= "</table>";

		return $html;
	}
	//添加条目
	function addItem(){
		if(isset($_POST['dev-type'])){
			//获取表单信息
			$type 		= strip_tags($_POST['dev-type']);
			$number 	= strip_tags($_POST['dev-number']);
			$product	= strip_tags($_POST['dev-product']);
			$info 		= strip_tags($_POST['dev-info']);
			$mac 		= strip_tags($_POST['dev-mac']);
			$os 		= strip_tags($_POST['dev-os']);
			$other 		= strip_tags($_PSOT['dev-other']);
			$version	= strip_tags($_POST['dev-version']);
			
			$info = str_replace(array(";","；"), "<br>", $info);
				
			$dev = new \Home\Common\Device();
			$status = $dev->addDevice($type, $number, $product, $info, $mac, $os, $version, $other);
		
			if($status) $this->redirect('/Home/Index/ajaxIndex');
			else 		$this->error('添加失败，请联系管理员！');
		}else $this->error("error(无法获取设备类型!)");
	}
	//搜索条目
	function sltItem($keyword){
		$dev = new \Home\Common\Device();
		$arDve = $dev->sltDevice($keyword);
		
		$html = "<table cellpadding='0' cellspacing='0' id='dev-table' class='info-table'>
				<tr><th colspan='9'>公司设备条目：</th></tr>
				<tr><th width='40'>序号</th>
				<th width='50'>类型</th>
				<th width='40'>型号</th>
				<th width='60'>厂商</th>
				<th width='80'>配置</th>
				<th width='80'>Mac地址</th>
				<th width='40'>系统</th>
				<th width='60'>版本</th>
				<th width='40'>备注</th></tr>";
		foreach($arDve as $aDev){
			$html .= "<tr>
			<td>$aDev[0]</td><td>$aDev[1]</td><td>$aDev[2]</td><td>$aDev[3]</td>
			<td>$aDev[4]</td><td>$aDev[5]</td><td>$aDev[6]</td><td>$aDev[7]</td>
			<td>$aDev[8]</td></tr>";
		}
		$html .= "</table>";

		return $html;
	}
}